package com.aisb.gis.util;

import com.aisb.core.util.file.TxtFileUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class LacciGeoHash {
    private int hashLength = 8;

    public static void main(String[] args) {
        List<String> lnglatList = lacciData();
        new LacciGeoHash().lacciGeoHash(lnglatList);
    }

    public void lacciGeoHash(List<String> lnglatList) {
        GeoHash gh = null;
        List<String> ghCenterSet = new ArrayList<>();

        for (int i = 0; i < lnglatList.size(); i++) {
            System.out.println("目前进度：" + i + "，" + (Math.round((float) i / lnglatList.size() * 100)) + "%");
            String lnglat = lnglatList.get(i).trim();
            if (StringUtils.isBlank(lnglat)) {
                continue;
            }
            lnglat = lnglat.replace(";", "");
            String lng = lnglat.substring(0, lnglat.indexOf(",")).trim();
            String lat = lnglat.substring(lnglat.indexOf(",") + 1).trim();
            if (lng.length() < 5 || lat.length() < 4) {
                continue;
            }

            gh = new GeoHash(Double.parseDouble(lng), Double.parseDouble(lat));
            gh.sethashLength(hashLength);

            String ghnum = gh.getGeoHashBase32();
            LocationBean lb = gh.getLocation(ghnum);
            if (null != lb) {
                double lngc = lb.getLng();
                double latc = lb.getLat();
                double width = lb.getWidth();
                double height = lb.getHeight();
                ghCenterSet.add(lng + "," + lat + "," + ghnum + "," + lngc + "," + latc + "," + ((width < height) ? width / 2 : height / 2) + "," + lb.getMaxLng() + "," + lb.getMinLng() + "," + lb.getMaxLat() + "," + lb.getMinLat() + "," + width + "," + height + ";");
//                ghCenterSet.add(ghnum + "," + lb.getLng() + "," + lb.getLat() + "," + sp * 111319.5 / 2 + ",;");
//                ghCenterSet.add(lb.getLng() + "," + lb.getLat() + "," + sp * 111319.5 / 2 + ";");
            }
        }

        String filePath = "C:\\Users\\Administrator\\Desktop\\dim_lac_cell_info\\dim_lac_cell_info测试_haslength" + hashLength + ".txt";
        if (ghCenterSet.size() > 20) {
            TxtFileUtil.write(filePath, ghCenterSet);
        } else {
            System.out.println(com.sun.deploy.util.StringUtils.join(ghCenterSet, "\n\r"));
        }
    }

    public static List<String> lacciData() {
        String filePath = "C:\\Users\\Administrator\\Desktop\\dim_lac_cell_info\\dim_lac_cell_info测试.csv";
        List<String> list = TxtFileUtil.readList(filePath);

        return list;
    }

    public static List<String> lacciData2() {
        List<String> list = new ArrayList<>();
        list.add("108.962222,34.259722");
        list.add("108.958277, 34.259536;");

        return list;
    }
}


